var jsonAddrs = null;
$(function(){
	clearForm();
	getListAddrs(true);	
	$("#address").change(function(){
		var selected = $(this).val();
		clearForm();
		if (jsonAddrs.length>0){
			for(var i=0;jsonAddrs.length;i++){
				if(selected == jsonAddrs[i].id){
					$("#id").val(jsonAddrs[i].id);
					$("#receiveName").val(jsonAddrs[i].receiveName);
					$("#fullAddress").val(jsonAddrs[i].fullAddress);
					$("#postalCode").val(jsonAddrs[i].postalCode);
					$("#phone").val(jsonAddrs[i].phone);
					$("#mobile").val(jsonAddrs[i].mobile);
				}
			}			
		}
	});
	
	$("#receiveName").blur(function(){
		checkReceiveName();
	});
	
	$("#fullAddress").blur(function(){
		checkFullAddress();
	});
	
	$("#postalCode").blur(function(){
		checkPostalCode();
	});
	
	$("#phone").blur(function(){
		checkPhone();
	});
	
	$("#mobile").blur(function(){
		checkMobile();
	});

	$("#addressForm").submit(function(){

		var ok = checkReceiveName()
				& checkFullAddress()
				& checkPostalCode()
				& checkPhone()
				& checkMobile();
		return ( ok=="0"?false:true);
	});
	
});
function checkReceiveName(){
	var receiveName = $("#receiveName").val();
	$("#nameErrorMsg").html("");
	if (receiveName == ""){
		$("#nameErrorMsg").html("收件人姓名不能为空");
		return false;
	}
	return true;
}

function checkFullAddress(){
	var fullAddress = $("#fullAddress").val();
	$("#addressErrorMsg").html("");
	if (fullAddress == ""){
		$("#addressErrorMsg").html("收件人地址不能为空");
		return false;
	}
	return true;
}

function checkPostalCode(){
	var postalCode = $("#postalCode").val();
	$("#postalCodeErrorMsg").html("");
	var pattern=/^[a-zA-Z0-9]{3,12}$/;
	if (postalCode == ""){
		$("#postalCodeErrorMsg").html("邮政编码不能为空");
		return false;
	}else if (!pattern.test(postalCode)){
		$("#postalCodeErrorMsg").html("邮政编码格式错误");
	}
	return true;
}

function checkPhone(){
	var phone = $("#phone").val();
	$("#phoneErrorMsg").html("");
	
	var pattern = /^(\+86|0086)?(\s*|\-)?\d{3,4}(\s*|\-)?[1-9]\d{2,8}$/;
	if (phone == ""){
		$("#phoneErrorMsg").html("电话号码不能为空");
		return false;
	}else if (!pattern.test(phone)){
		$("#phoneErrorMsg").html("电话号码格式错误");
	}
	return true;
}

function checkMobile(){
	var mobile = $("#mobile").val();
	$("#mobileErrorMsg").html("");
	
	var pattern = /^(\+86|0086)?\s{0,3}?0?[0-9]{11}$/;
	if (mobile == ""){
		$("#mobileErrorMsg").html("手机号码不能为空");
		return false;
	}else if (!pattern.test(mobile)){
		$("#mobileErrorMsg").html("手机号码格式错误");
	}
	return true;
}


function clearForm(){
	$("#id").val("-1");
	$("#receiveName").val("");
	$("#fullAddress").val("");
	$("#postalCode").val("");
	$("#phone").val("");
	$("#mobile").val("");
}

function getListAddrs(async){
	$.ajax({
		"url":"../user/receiveAddrs",
		"type":"post",
		"data":{"userId":$("#userId").val()},
		"dataType":"json",
		"success":function(listAddrs,status){
			jsonAddrs = eval(listAddrs);
			for(var i=0;jsonAddrs.length;i++){
				$("#address").append("<option value='"+jsonAddrs[i].id+"'>"+jsonAddrs[i].receiveName+"</option>");
			}
		},
		"async":async});
}